﻿using System.Collections.Generic;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using ServiceStack.Text;
using SoNerdy.Player.Models;

namespace SoNerdy.Player.Drivers {
    public class PlayerPartDriver: ContentPartDriver<PlayerPart>{
        private readonly JsonSerializer<IEnumerable<PlayerMedia>> _serializer;
        protected override string Prefix
        {
            get { return "Player"; }
        }
        
        public PlayerPartDriver() {
            _serializer = new JsonSerializer<IEnumerable<PlayerMedia>>();
        }

        protected override DriverResult Display(PlayerPart part, string displayType, dynamic shapeHelper) {
            var media = _serializer.DeserializeFromString(part.Data);
            if (media==null)
                media = new List<PlayerMedia>();
            
            return Combined(
                ContentShape("Parts_Player",
                             () =>
                             {                                 
                                 return shapeHelper.Parts_Player(Media: media, ContentItem: part.ContentItem);
                             }),
                ContentShape("Parts_Player_Summary",
                             () =>
                             {
                                 return shapeHelper.Parts_Player_Summary(Media: media, ContentItem: part.ContentItem);
                             })
                );
        }

        protected override DriverResult Editor(PlayerPart part, dynamic shapeHelper) {            
            var json = part.Data;
            if (string.IsNullOrWhiteSpace(json)) {                
                json = _serializer.SerializeToString(new List<PlayerMedia>());
            }
            var viewModel = new PlayerMediaViewModel() {MediaJSON = json};
            return ContentShape("Parts_Player_Edit", () =>
                    shapeHelper.EditorTemplate(
                        TemplateName: "Parts.Player",                        
                        Prefix: Prefix,
                        Model: viewModel));
        }

        protected override DriverResult Editor(PlayerPart playerPart, IUpdateModel updater, dynamic shapeHelper) {
            var viewModel = new PlayerMediaViewModel();
            updater.TryUpdateModel(viewModel, Prefix, null, null);
            
            playerPart.Data = viewModel.MediaJSON;
            return Editor(playerPart, shapeHelper);
        }

        protected override void Importing(PlayerPart part, Orchard.ContentManagement.Handlers.ImportContentContext context)
        {            
            var data = context.Attribute(part.PartDefinition.Name, "Data");
            if (data != null)
            {
                part.Data = data;
            }
        }

        protected override void Exporting(PlayerPart part, Orchard.ContentManagement.Handlers.ExportContentContext context)
        {
            context.Element(part.PartDefinition.Name).SetAttributeValue("Data", part.Data);                  
        }
    }
}